package com.hst.web.base.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.hst.core.ServiceContext;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.web.auth.po.User;
import com.hst.web.base.entity.TAccountIdentity;
import com.hst.web.base.entity.TAccountInfo;
import com.hst.web.base.entity.TCompanyEmployee;
import com.hst.web.base.service.RegisterService;
import com.hst.web.base.ui.Register;
import com.hst.web.sys.entity.TUser;

@Controller
@RequestMapping("/register")
public class RegisterController {
	
	@Autowired
	IORDao dao;
	
	@Autowired
	RegisterService service;
	
	@SuppressWarnings({ "unchecked" })
	private Map<String, Object> parseQuerys(String data) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> querys = JsonUtil.read(data, Map.class);
		return querys;
	}
	
	@RequestMapping("/check")
	public void check(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = parseQuerys(data);
		String account = (String) params.get("account");
		List<ORQuery> querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq,"operatorNbr",account));
		List<TUser> users = dao.list(TUser.class,querys);
		querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq,"userName",account));
		List<TAccountIdentity> identities = dao.list(TAccountIdentity.class,querys);
		if(users.size() > 0 || identities.size() > 0) {
			m.addAttribute("success","double");
		}else {
			m.addAttribute("success","success");
		}
	}
	
	@RequestMapping("/people-check")
	public void peopleCheck(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = parseQuerys(data);
		String fullname = (String) params.get("fullname");
		String papersType = (String) params.get("papersType");
		String papersNumber = (String) params.get("papersNumber");
		List<ORQuery> querys = new ArrayList<ORQuery>();
		//querys.add(new ORQuery(Op.eq,"fullname",fullname));
		querys.add(new ORQuery(Op.eq,"papersType",papersType));
		querys.add(new ORQuery(Op.eq,"papersNumber",papersNumber));
		List<TCompanyEmployee> employees = dao.list(TCompanyEmployee.class,querys);
		if(employees.size() > 0) {
			// 检测当前员工是否有过实名认证 或者 有用户账号
			querys = new ArrayList<ORQuery>();
			querys.add(new ORQuery(Op.eq,"employeeCode",employees.get(0).getEmployeeCode()));
			List<TUser> users = dao.list(TUser.class,querys);
			List<TAccountIdentity> inds  = dao.list(TAccountIdentity.class,querys);
			if(users.size() > 0 || inds.size() > 0) {
				m.addAttribute("success","hasuser");
			}else {
				m.addAttribute("success","success");
			}
		}else {
			m.addAttribute("success","noemployee");
		}
	}
	
	@RequestMapping("/register")
	public void register(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Register reg = JsonUtil.read(data, Register.class);
		List<ORQuery> querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq,"operatorNbr",reg.getAccount()));
		List<TUser> users = dao.list(TUser.class,querys);
		querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq,"userName",reg.getAccount()));
		List<TAccountIdentity> identities = dao.list(TAccountIdentity.class,querys);
		if(users.size() > 0 || identities.size() > 0) {
			m.addAttribute("msg","账号重复");
			return;
		}
		querys = new ArrayList<ORQuery>();
		//querys.add(new ORQuery(Op.eq,"fullname",reg.getFullname()));
		querys.add(new ORQuery(Op.eq,"papersType",reg.getPapersType()));
		querys.add(new ORQuery(Op.eq,"papersNumber",reg.getPapersNumber()));
		List<TCompanyEmployee> employees = dao.list(TCompanyEmployee.class,querys);
		if(employees.size() == 0) {
			m.addAttribute("msg","该人员不存在");
			return;
		}else {
			querys = new ArrayList<ORQuery>();
			querys.add(new ORQuery(Op.eq,"employeeCode",employees.get(0).getEmployeeCode()));
			List<TUser> users1 = dao.list(TUser.class,querys);
			List<TAccountIdentity> inds  = dao.list(TAccountIdentity.class,querys);
			if(users1.size() > 0 || inds.size() > 0) {
				m.addAttribute("msg","该人员已有账号或进行过实名认证");
			}
		}
		service.register(reg, employees.get(0),m);
	}
	
	@RequestMapping("/load-person")
	public void loadPerson(Model m) throws JsonGenerationException, JsonMappingException, IOException {
		User u = (User) ServiceContext.getInstance().getUser();
		TUser user = dao.get(TUser.class, u.getId());
		TAccountIdentity ind = dao.get(TAccountIdentity.class, user.getOperatorNbr());
		TAccountInfo info = dao.get(TAccountInfo.class,user.getOperatorNbr());
		m.addAttribute("ind",JsonUtil.toString(ind));
		m.addAttribute("info",JsonUtil.toString(info));
	}
	
	@RequestMapping("/save")
	public void save(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = parseQuerys(data);
		TAccountIdentity ind = JsonUtil.read((String) params.get("ind"), TAccountIdentity.class);
		TAccountInfo info = JsonUtil.read((String) params.get("info"), TAccountInfo.class);
		service.save(ind, info, m);
	}
}
